<template>
    <div class="">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" :model="queryParams" :inline="true">
                <el-form-item label="状态" prop="is_show">
                    <el-select
                        class="w-[10.75rem]"
                        v-model="queryParams.is_show"
                        placeholder="请选择状态"
                    >
                        <el-option label="显示" :value="1" />
                        <el-option label="隐藏" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">搜索</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <router-link
                    v-perms="['banner.banner/bannerAdd/add', 'banner.banner/bannerAdd/add:edit']"
                    :to="{
                        path: getRoutePath('banner.banner/bannerAdd/add:edit')
                    }"
                >
                    <el-button type="primary" class="mb-4">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        添加banner
                    </el-button>
                </router-link>
            </div>
            <el-table size="large" :data="pager.lists">
                <el-table-column label="ID" prop="banner_id" min-width="80" />
                <el-table-column label="banner图" min-width="180">
                    <template #default="{ row }">
                        <el-image
                            v-if="row.banner_url"
                            :src="row.banner_url"
                            style="width: 200px"
                            fit="contain"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="状态" min-width="120">
                    <template #default="{ row }">
                        <el-tag type="success" v-if="row.status == 1">显示</el-tag>
                        <el-tag type="danger" v-if="row.status == 2">隐藏</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="排序" prop="sort" min-width="80" />
                <el-table-column label="创建时间" prop="create_time" min-width="120" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="[
                                'banner.banner/bannerAdd/add',
                                'banner.banner/bannerAdd/add:edit'
                            ]"
                            type="primary"
                            link
                        >
                            <router-link
                                :to="{
                                    path: getRoutePath('banner.banner/bannerAdd/add:edit'),
                                    query: {
                                        id: row.banner_id
                                    }
                                }"
                            >
                                编辑
                            </router-link>
                        </el-button>
                        <el-button type="danger" link @click="handleDelete(row.banner_id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="articleLists">
import { usePaging } from '@/hooks/usePaging'
import { bannerList, deleteBanner } from '@/api/banner'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'
const queryParams = reactive({
    is_show: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: bannerList,
    params: queryParams
})

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await deleteBanner({ banner_id: id })
    getLists()
}

getLists()
</script>
